<template>
    <div>
        <el-alert title="您开户申请已通过，请立即上传资料！" type="success" show-icon class="perupload-icon" />
        <el-form class="perupload-main" ref="formRef" :rules="rules" :model="form" label-width="auto">
            <el-row class="zw-form-item">
                <div>
                    <el-form-item
                        label="营业执照图片"
                        prop="licenseImg"
                        :error="uploadErrMsg.licenseImg"
                        class="item-content">
                        <UploadImg v-model:imageUrl="form.licenseImg" :fileSize="4" tip="(JPG，JPEG，PNG，不超过4M)" />
                    </el-form-item>
                </div>
                <div>
                    <el-form-item
                        label="法人身份证人像面"
                        prop="corporateFrontImg"
                        class="item-content"
                        :error="uploadErrMsg.corporateFrontImg">
                        <UploadImg
                            v-model:imageUrl="form.corporateFrontImg"
                            :fileSize="2"
                            tip="(JPG，JPEG，PNG，不超过2M)" />
                    </el-form-item>
                </div>
                <div>
                    <el-form-item
                        label="法人身份证国徽面"
                        class="item-content"
                        prop="corporateBackImg"
                        :error="uploadErrMsg.corporateBackImg">
                        <UploadImg
                            v-model:imageUrl="form.corporateBackImg"
                            :fileSize="2"
                            tip="(JPG，JPEG，PNG，不超过2M)" />
                    </el-form-item>
                </div>
            </el-row>
            <el-row>
                <div v-if="props.query.proposer == 2">
                    <el-form-item
                        label="代理人身份证人像面"
                        prop="agentFrontImg"
                        class="item-content"
                        :error="uploadErrMsg.agentFrontImg"
                        :rules="{
                            required: props.query.proposer == 2,
                            message: '请上传代理人身份证头像图片',
                            trigger: 'blur'
                        }">
                        <UploadImg
                            v-model:imageUrl="form.agentFrontImg"
                            :fileSize="2"
                            tip="(JPG，JPEG，PNG，不超过2M)" />
                    </el-form-item>
                </div>
                <div v-if="props.query.proposer == 2">
                    <el-form-item
                        label="代理人身份证国徽面"
                        prop="agentBackImg"
                        class="item-content"
                        :error="uploadErrMsg.agentBackImg"
                        :rules="{
                            required: props.query.proposer == 2,
                            message: '请上传代理人身份证国徽图片',
                            trigger: 'blur'
                        }">
                        <UploadImg
                            v-model:imageUrl="form.agentBackImg"
                            :fileSize="2"
                            tip="(JPG，JPEG，PNG，不超过2M)" />
                    </el-form-item>
                </div>
                <div v-if="props.query.proposer == 2">
                    <el-form-item
                        label="代理人委托书"
                        class="item-content"
                        prop="agentPaper"
                        :error="uploadErrMsg.agentPaper"
                        :rules="{
                            required: props.query.proposer == 2,
                            message: '请上传代理人委托书',
                            trigger: 'blur'
                        }">
                        <UploadImg v-model:imageUrl="form.agentPaper" :fileSize="4" tip="(JPG，JPEG，PNG，不超过4M)" />
                    </el-form-item>
                    <span class="custom-down" @click="downloadFileHandle">下载文件</span>
                </div>
            </el-row>
            <el-row>
                <div>
                    <el-form-item label="特殊资质证书" :error="uploadErrMsg.specialCertificate" class="item-content">
                        <UploadImg
                            v-model:imageUrl="form.specialCertificate"
                            :fileSize="4"
                            tip="(JPG，JPEG，PNG，不超过4M)" />
                    </el-form-item>
                </div>
            </el-row>
        </el-form>

        <div class="footer">
            <div @click="submitHandler" class="sumbit-btn">上传资料</div>
        </div>
        <div class="updoc-status" v-if="status === 1">
            <img class="tip-img" src="@/assets/imgs/sys/pay-updoc-auth.png" />
            <span class="tip-text">资料已提交，待平台审核！</span>
            <el-button type="primary" @click="submitHandler">提交开通</el-button>
        </div>
    </div>
</template>
<script setup>
import { ref, reactive } from 'vue';
import request from '@/request';
import { ElLoading, ElMessage } from 'element-plus';
import { cloneDeep, downloadImgHandler } from '@/commons/utils';
import UploadImg from '@/components/UploadImg.vue';

const emit = defineEmits(['stepChange']);
const props = defineProps({
    query: { type: Object, default: () => ({}) }
});
const formRef = ref(null);
const form = reactive({
    /**上一步新增的id！！！！--待确认是否要传 */
    id: props.query.id,
    /** 营业执照*/
    licenseImg: '',
    /** 法人身份证头像*/
    corporateFrontImg: '',
    /** 法人身份证国徽图片*/
    corporateBackImg: '',
    /** 代理人身份证头像*/
    agentFrontImg: '',
    /** 代理人身份证国徽图片*/
    agentBackImg: '',
    /** 代理人委托书*/
    agentPaper: '',
    /**特殊资质证书 */
    specialCertificate: ''
});
const uploadErrMsg = reactive({
    /** 营业执照*/
    licenseImg: '',
    /** 法人身份证头像*/
    corporateFrontImg: '',
    /** 法人身份证国徽图片*/
    corporateBackImg: '',
    /** 代理人身份证头像*/
    agentFrontImg: '',
    /** 代理人身份证国徽图片*/
    agentBackImg: '',
    /** 代理人委托书*/
    agentPaper: '',
    /**特殊资质证书 */
    specialCertificate: ''
});
const rules = reactive({
    licenseImg: [{ required: true, message: '请上传营业执照图片', trigger: 'blur' }],
    corporateFrontImg: [{ required: true, message: '请上传法人身份证人像面', trigger: 'blur' }],
    corporateBackImg: [{ required: true, message: '请上传法人身份证国徽面', trigger: 'blur' }]
});
const status = ref(0);

const submitHandler = async () => {
    await formRef.value.validate();
    const loading = ElLoading.service({
        lock: true,
        text: '上传资料中',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    form.loginNo = props.query.loginNo;
    const newForm = cloneDeep(form);
    const { code, msg } = await request('baofuOpenUpload', newForm);
    if (code === 0) {
        emit('stepChange');
    } else {
        ElMessage.error(msg);
    }
    loading.close();
};
// 下载代理人委托书
const downloadFileHandle = async () => {
    const { data } = await request('mandateServ');
    downloadImgHandler(data.url, '代理人委托书');
};
const imgChangeHandler = ({ imgUrl, msg }, prop) => {
    form[prop] = imgUrl || '';
    uploadErrMsg[prop] = msg || '';
    imgUrl && formRef.value.validateField(prop);
};
</script>
<style lang="scss" scoped>
.perupload-icon {
    margin: 22px 0 24px 0;
}
.perupload-main {
    margin-top: 24px;
    .custom-tips {
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #999999;
        margin-top: 8px;
        line-height: 12px;
    }
    .el-row {
        justify-content: space-between;
        margin-bottom: 22px;
    }
    .custom-down {
        cursor: pointer;
        position: absolute;
        top: 0;
        line-height: 32px;
        font-size: 14px;
        right: 0;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ff6b00;
    }
    .item-content :deep(.el-form-item__content) {
        flex-direction: column;
        align-items: flex-start;
        .img-uploader {
            width: 120px;
            height: 120px;
            .has-upload-img,
            .img-uploader-icon {
                width: 120px;
                height: 120px;
            }
        }
    }
    :deep(.is-required .el-form-item__content) {
        margin-left: 9px !important;
    }

    .el-form-item--default {
        margin-bottom: 0px;
    }
    .el-form-item {
        position: relative;
        flex-direction: column;
    }
    .item-content :deep(.el-form-item__label-wrap) {
        margin-left: 0 !important;
    }
}

.zw-el-form :deep(.el-form-item__error) {
    white-space: nowrap;
}

.footer {
    display: flex;
    justify-content: center;
    width: 100%;
    text-align: left;
    box-sizing: border-box;
    margin-top: 115px;
    .sumbit-btn {
        width: 168px;
        height: 32px;
        line-height: 32px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        background: #ff6b00;
        border-radius: 4px;
        text-align: center;
        cursor: pointer;
    }
}
.divider {
    height: 2px;
    background-color: #eaeaea;
    margin: 30px 0;
}
.updoc-status {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 100px;

    .tip-img {
        width: 80px;
        height: 80px;
    }

    .tip-text {
        margin-top: 40px;
        margin-bottom: 24px;
        font-size: 14px;
        font-weight: 300;
        color: #33302d;

        .err-info {
            color: #4a90e2;
        }
    }
}

.el-popper.updoc-popper {
    .ul {
        display: flex;
        flex-direction: column;

        .li {
            line-height: 19px;
            font-size: 14px;
            font-weight: 300;
            color: #33302d;
        }
    }
}
</style>
